<?php
//连接数据库操作
$conn=mysql_connect("localhost","root","123456");
mysql_select_db("lxtx",$conn);
mysql_query('set names utf8');
error_reporting(0); 

session_start();

//定量
$APPID   = 'wx9692a5d1ecfa16b4';
$MCHID   = '1319572501';
$KEY     = '12345678901234567890123456789012';
$APPSECRET = '3683b66fa5708cdf0802d29f7e2314e2';

//token 
$tuijian      =   $_REQUEST['tuijian']?$_REQUEST['tuijian']:'';//推荐标识符，判断用户进入的链接，是否是用户分享出来的

$log_out      =   $_REQUEST['log_out']?$_REQUEST['log_out']:'';//退出
if(strlen($tuijian)>0){
}
else if(strlen($log_out)>0){ //退出
	session_destroy();
	setcookie('token','',time()-3600);
	$url="http://haining.365jinbi.com/lxtx/mobile/index.php?m=default&c=user&a=login";
	echo "<SCRIPT>window.location.href='$url';</SCRIPT>";
	exit;
}else{
	$token      =   $_REQUEST['token'];
	if(!empty($token))
	{
		$_SESSION['openid']='';
		$_SESSION['user_id']='';

		$time=time();
		$sql="select * from `ecs_users` where token='$token' ";//and expire_in>$time
		$query = mysql_query($sql);
		while($row = mysql_fetch_array($query)){
			$openid=$row['openid'];
			$_SESSION['openid']   =   $openid; 
			$user_id=$row['user_id'];
			$_SESSION['user_id']  =   $user_id;
            
            $expire = time() + 3600*24*60;
			setcookie('token',$token,$expire);
		}
		//获取session
		//$user_id     =   $_SESSION['user_id'];//user_id

	}


}






//活动未登录--检测微信对应是否有账号
if(empty($_SESSION['user_id'])&&empty($_SESSION['check_openid']))
{

	//cookie 读取用户
    $token=$_COOKIE['token'];
    if($token)
    {
    	$time=time();
		$sql="select * from `ecs_users` where token='$token' ";//and expire_in>$time
		$query = mysql_query($sql);
		while($row = mysql_fetch_array($query)){
			$openid=$row['openid'];
			$_SESSION['openid']   =   $openid; 
			$user_id=$row['user_id'];
			$_SESSION['user_id']  =   $user_id;
            
            $expire = time() + 3600*24*60;
			setcookie('token',$token,$expire);
		}
    }
    
    if(empty($user_id))
    {
		$this_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$_SESSION['referer_url']=$this_url;
		$_SESSION['check_openid']=1;

		echo "<SCRIPT>window.location.href='index.php';</SCRIPT>";
		exit;    	
    }


}

$openid     =   $_SESSION['openid'];     //用户openid
$id_difang  =   $_SESSION['id_difang'];	 //用户所选路线标识符
$id_main	=	$_SESSION['id_main'];    //订单标识符
$is_xiugai 	= 	$_SESSION['is_xiugai'];  //个人中心是否显示返回订单标识
$user_id    =   $_SESSION['user_id'];

$time 	    = 	$_SESSION['time'];    //时间筛选
$xiangmu 	= 	$_SESSION['xiangmu']; //项目筛选
$banqi 	    = 	$_SESSION['banqi'];   //班期筛选
$is_zhifu   = 	$_SESSION['is_zhifu'];//支付状态筛选

//更新session 以防页面过期
$_SESSION['openid']   =   $openid;      
$_SESSION['id_difang']=   $id_difang;	  
$_SESSION['id_main']  =   $id_main;     
$_SESSION['is_xiugai']=   $is_xiugai;   

$_SESSION['time']     =   $time;
$_SESSION['xiangmu']  =   $xiangmu;
$_SESSION['banqi']    =   $banqi;
$_SESSION['is_zhifu']=   $is_zhifu;  

//PHP函数



//js函数
?>